package com.zrrd.learn.chapter06数组;

import java.util.Arrays;

/**
 * 数组练习3
 *
 * @author wrn
 * @version 1.0
 * @date 2023/7/14 13:21
 */

public class ArrayLianxi03 {


    /**
     * 请将1,9,2,5,4,8,3,6,7,10,8存入数组a，然后输出打印；
     */
    public static void question1() {

        System.out.printf("1题\n");
        int[] a = {1, 9, 2, 5, 4, 8, 3, 6, 7, 10, 8};
        System.out.println(Arrays.toString(a));

    }

    /**
     * 将数组a中的数字进行从小到大和从大到小排序输出，分别打印；a = 1,9,2,5,4,8,3,6,7,10,8
     */
    public static void question2() {

        System.out.printf("2题\n");
        int[] a = {1, 9, 2, 5, 4, 8, 3, 6, 7, 10, 8};
        System.out.println("原数组" + Arrays.toString(a));
        Arrays.sort(a);
        System.out.println("从小到大排序" + Arrays.toString(a));

        int sum = 0;
        for (int i = 0; i < a.length; i++) {
            for (int j = i + 1; j < a.length; j++) {
                if (a[i] < a[j]) {
                    sum = a[i];
                    a[i] = a[j];
                    a[j] = sum;
                }
            }
        }
        System.out.println("从大到小排序" + Arrays.toString(a));

    }

    /**
     * 查找数组a中的最大值、最小值，并打印输出它们的下标位置；
     */
    public static void question3() {

        System.out.printf("3题\n");
        int[] a = {1, 9, 2, 5, 4, 8, 3, 6, 7, 10, 8};
        int maxarray = a[0];
        int maxarrayindex = 0;
        for (int i = 0; i < a.length; i++) {
            if (maxarray < a[i]){
                maxarray = a[i];
                maxarrayindex = i;
            }
        }
        System.out.println("最大数的数组下标为"+maxarrayindex);

        int minarray = a[0];
        int minarrayindex = 0;
        for (int i = 0; i < a.length; i++) {
            if (minarray > a[i]){
                minarray = a[i];
                minarrayindex = i;
            }
        }
        System.out.println("最小数的数组下标为"+minarrayindex);

    }

    /**
     * 定义一个数组来存储12个学生的成绩{72,89,65,58,87,91,53,82,71,93,76,68}，计算并输出学生的平均成绩。(保留小数点后两位。)
     */
    public static void question4() {

        System.out.printf("4题\n");
        int sum = 0;
        double avg = 0;
        int[] a = {72,89,65,58,87,91,53,82,71,93,76,68};
        for (int i = 0; i < a.length; i++) {
            sum+=a[i];
        }
        avg=sum/a.length;
        System.out.println("总成绩为"+sum);
        System.out.println("平均分为"+String.format("%.2f",avg));


    }

    /**
     * 统计数组{20,45,78,34,16,3,99,56}中大于50的有多少个、小于50的有多少个并打印输出。
     */
    public static void question5() {

        System.out.printf("5题\n");
        int[] a = {20,45,78,34,16,3,99,56};
        int big =0;
        int small =0;
//        int[] bigarr = new int[5];
//        int[] smallarr = new int[5];
        for (int i = 0; i < a.length; i++) {
            if (a[i]>50){
                System.out.print(a[i]+"\t\n");
                big+=1;

            }
            if (a[i]<50){
                System.out.print(a[i]+"\t\n");
                small+=1;
            }
        }
        System.out.println("数组中大于50有的"+big+"个\n"+"数组中小于50的有"+small+"个");
//        System.out.println("大于50的数组"+Arrays.toString(bigarr)+"小于50的数组"+Arrays.toString(smallarr));
    }
    public static void main(String[] args) {
        question1();
        question2();
        question3();
        question4();
        question5();
    }

}
